import { update@{modelName} } from '@/services/@{serviceName}Service';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import { message, Modal } from 'antd';
import React, { PropsWithChildren } from 'react';

interface UpdateModalProps
{
  oldData: @{modelName}Type.@{modelName};
  modalVisible: boolean;
  columns: ProColumns<@{modelName}Type.@{modelName}>[];
  onSubmit: () => void;
  onCancel: () => void;
  typeList: any[];
}

const handleUpdate = async (fields: @{modelName}Type.@{modelName}) =>
{
  const hide = message.loading('正在更新');
  try
  {
    await update@{modelName}(fields);
    hide();

    message.success('更新成功');
    return true;
  } catch (error)
  {
    hide();
    message.error('更新失败请重试！');
    return false;
  }
};

const UpdateModal: React.FC<PropsWithChildren<UpdateModalProps>> = (props) =>
{
  let { oldData, columns, modalVisible, onSubmit, onCancel } = props;

  return (
    <Modal
      destroyOnClose
      title="更新"
      open={modalVisible}
      onCancel={() => onCancel()}
      footer={null}
    >
      <ProTable<@{modelName}Type.@{modelName}, @{modelName}Type.@{modelName}>
        onSubmit={async (values) =>
        {
          const success = await handleUpdate({
            ...oldData,
            ...values,
          });
          if (success)
          {
            onSubmit?.();
          }
        }}
        rowKey="id"
        type="form"
        form={{
          initialValues: oldData,
        }}
        columns={columns}
      />
    </Modal>
  );
};

export default UpdateModal;
